package cn.wolfcode.shop;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IProductSkuService;
import cn.wolfcode.shop.service.ISkuPropertyService;
import cn.wolfcode.shop.service.ISkuPropertyValueService;
import cn.wolfcode.shop.vo.GenerateSkuVo;
import cn.wolfcode.shop.vo.JSONResultVo;
import cn.wolfcode.shop.vo.ProductVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class ProductSkuController {

    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private IProductService productService;
    @Reference
    private ISkuPropertyValueService skuPropertyValueService;
    @Reference
    private IProductSkuService productSkuService;

    @RequestMapping("/productSku")
    public String productSkuPage(Long productId, Model model){
        Product product = productService.getProductById(productId);
        model.addAttribute("product", product);
        List<ProductSku> productSkuList = productSkuService.getByProductId(productId);
        List<SkuProperty> skuPropertyList = new ArrayList<SkuProperty>();
        if (productSkuList.size() > 0) {
            //skuPropertyList
            //ProductSkuList
            for (ProductSkuProperty psp : productSkuList.get(0).getProductSkuPropertyList()) {
                skuPropertyList.add(psp.getSkuProperty());
            }
            model.addAttribute("skuPropertyList", skuPropertyList);
            model.addAttribute("skuList", productSkuList);
            return "product_sku/sku_list";
        }
        model.addAttribute("skuPropertyList", skuPropertyService.listByCatalogId(product.getCatalog().getId()));
        return "product_sku/generate_sku";
    }

    @RequestMapping("/productSku/getSkuPropertyValue")
    public String SkuPropertyValuePage(Long skuPropertyId, Model model){
        SkuProperty skuProperty = skuPropertyService.get(skuPropertyId);
        model.addAttribute("skuProperty", skuProperty);
        List<SkuPropertyValue> skuPropertyValueList = skuPropertyValueService.selectByPId(skuProperty.getId());
        model.addAttribute("skuPropertyValueList", skuPropertyValueList);
        return "product_sku/sku_property_value_table";
    }

    @RequestMapping("/productSku/generateSku")
    public String generateSku(@RequestBody GenerateSkuVo generateSkuVo, Model model){
//        System.out.println(generateSkuVo);
        model.addAttribute("skuPropertyList", generateSkuVo.getSkuPropertyList());
        List<Map> skuList =  productSkuService.generateSku(generateSkuVo);
        model.addAttribute("skuList", skuList);
        return "skuProperty/gengrateSkuList";
    }
    @RequestMapping("/productSku/save")
    @ResponseBody
    public JSONResultVo save(ProductVo productVo){
        JSONResultVo vo = new JSONResultVo();
        try {
            productSkuService.save(productVo);
        } catch (Exception e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        }
        return vo;
    }

}
